package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.Grade;
import com.wzcl.app.dao.model.TestPaper;
import com.wzcl.app.dao.model.WorkShow;
import com.wzcl.app.dao.repository.TestPaperRepository;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.TestPaperDto;
import com.wzcl.app.model.dto.WorkShowDto;
import com.wzcl.app.model.vo.TestPaperVo;
import com.wzcl.app.service.TestPaperService;
import com.wzcl.app.utils.BeanUtil;
import com.wzcl.app.utils.DateUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2021年01月06日 16:08:00
 */
@Service
public class TestPaperServiceImpl implements TestPaperService {
    @Resource
    private TestPaperRepository testPaperRepository;

    @Override
    public TestPaper createTestPaper(TestPaperDto testPaperDto) {
        TestPaper testPaper = BeanUtil.convertBean(testPaperDto, new TestPaper().getClass());
        Date time = DateUtil.getDateBySystemTime(System.currentTimeMillis());
        testPaper.setCreateTime(time);
        testPaper.setUpdateTime(time);
        TestPaper paper = testPaperRepository.save(testPaper);
        //将数据模型转换为视图对象返回
        return paper;
    }

    @Override
    @Transactional
    public RVO deleteByIds(List<Integer> ids) {
        System.out.println("进来了");
        if (ids.size() == 0){
            RVO.error("传入id不能为空");
        }
        for (int index = 0; index < ids.size(); index++) {
            testPaperRepository.deleteById(ids.get(index));
        }
        return RVO.success("删除成功");
    }

    @Override
    public MyPage retrieveByCondition(TestPaperDto testPaperDto) {
        Page all = testPaperRepository.findAll(getSpecification(testPaperDto), testPaperDto.getPageable());
        return new MyPage<>(all);
    }

    @Override
    public TestPaper retrieveById(Integer id) {
        TestPaper paper = testPaperRepository.findPaperById(id);
        return paper;
    }

    @Override
    public RVO updateDownloadNumber(Integer id) {
        TestPaper testPaper = testPaperRepository.findPaperById(id);
        testPaperRepository.updateDownloadNumber(id,testPaper.getDownloadNumber()+1);
        return RVO.success("下载量更新成功");
    }

    public Specification<TestPaper> getSpecification(TestPaperDto testPaperDto){
        return (Specification<TestPaper>) (root, criteriaQuery, criteriaBuilder) -> {
            ArrayList<Predicate> list = new ArrayList<>();
            //条件过滤
            if (testPaperDto.getName() != null && testPaperDto.getName() != ""){
                //添加条件(试卷名称)
                list.add(criteriaBuilder.equal(root.get("name"), testPaperDto.getName()));
            }
            if (testPaperDto.getPaperType() != null && testPaperDto.getPaperType() != ""){
                //添加条件(试卷类型)
                list.add(criteriaBuilder.equal(root.get("paperType"), testPaperDto.getPaperType()));
            }
            if (testPaperDto.getGradeName() != null && testPaperDto.getGradeName() != ""){
                //添加条件(试卷年级)
                list.add(criteriaBuilder.equal(root.get("gradeName"), testPaperDto.getGradeName()));
            }
            if (testPaperDto.getSubject() != null && testPaperDto.getSubject() != ""){
                //添加条件(试卷学科)
                list.add(criteriaBuilder.equal(root.get("subject"), testPaperDto.getSubject()));
            }
            // 排序
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("updateTime")));
            // 将两个查询条件联合起来之后返回Predicate对象
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
    }
}